// pages/shopping/shopping.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectAllStatus: false,
    total: 199,
    list: [
      {
        icon: "/images/index.png",
        name: "浪漫满屋",
        selected: false,
        list: [
          {
            icon: "/images/c-bg-1.png",
            name: "满杯百果香",
            sub: "规格：中杯",
            price: 19,
            count: 2
          },
          {
            icon: "/images/c-bg-1.png",
            name: "满杯百果香",
            sub: "规格：中杯",
            price: 19,
            count: 2
          }
        ]
      },
      {
        icon: "/images/index.png",
        name: "浪漫满屋",
        selected: false,
        list: [
          {
            icon: "/images/c-bg-1.png",
            name: "满杯百果香",
            sub: "规格：中杯",
            price: 19,
            count: 2
          },
          {
            icon: "/images/c-bg-1.png",
            name: "满杯百果香",
            sub: "规格：中杯",
            price: 19,
            count: 2
          }
        ]
      }
    ]
  },
  selectItem(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.list;
    const selected = carts[index].selected;
    carts[index].selected = !selected;
    this.setData({
      list: carts
    });
    this.getTotalPrice();
  },
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;
    let carts = this.data.list;

    for (let i = 0; i < carts.length; i++) {
      carts[i].selected = selectAllStatus;
    }
    this.setData({
      selectAllStatus: selectAllStatus,
      list: carts
    });
    this.getTotalPrice();
  },
  /**
   * 绑定加数量事件
   */
  addCount(e) {
    const pindex = e.currentTarget.dataset.pindex;
    const index = e.currentTarget.dataset.index;
    let list = this.data.list;
    let carts = list[pindex].list;
    let num = carts[index].count;
    num = num + 1;
    carts[index].count = num;
    this.setData({
      list: list
    });
    this.getTotalPrice();
  },
  /**
   * 绑定减数量事件
   */
  minusCount(e) {
    const pindex = e.currentTarget.dataset.pindex;
    const index = e.currentTarget.dataset.index;
    let list = this.data.list;
    let carts = list[pindex].list;
    let num = carts[index].count;
    if (num <= 1) {
      return false;
    }
    num = num - 1;
    carts[index].count = num;
    this.setData({
      list: list
    });
    this.getTotalPrice();
  },
  deleteItem(e) {
    const pindex = e.currentTarget.dataset.pindex;
    const index = e.currentTarget.dataset.index;
    let list = this.data.list;
    var carts = list[pindex].list;
    carts.splice(index,1);
    if (carts.length == 0){
      list.splice(pindex, 1);
    }
    this.setData({
      list: list
    });
    this.getTotalPrice();
  },
  /**
   * 计算总价
   */
  getTotalPrice() {
    let carts = this.data.list;
    var total = 0;
    for (var i = 0; i < carts.length; i++) {
      let cart = carts[i];
      if (cart.selected) {
        for (var j = 0; j < cart.list.length; j++) {
          total += cart.list[j].count * cart.list[j].price;
        }
      }
    }
    this.setData({
      total: total.toFixed(2)
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getTotalPrice();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})